﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Helper;
using UIWindow.Lucifer;

namespace UIWindow.Protocols.Fins
{
    public class ResponseShakeHands : IProtocol
    {
        /// <summary>
        /// 标头
        /// </summary>
        public string Head { get; set; }
        /// <summary>
        /// 包长度
        /// </summary>
        public int DataLength { get; set; }
        /// <summary>
        /// 命令码
        /// </summary>
        public int Cmd { get; set; }
        /// <summary>
        /// 错误代码
        /// </summary>
        public int Error { get; set; }
        /// <summary>
        /// PLC网络标识
        /// </summary>
        public int ServerAddress { get; set; }
        /// <summary>
        /// PLC单元号
        /// </summary>
        public int ClientAddress { get; set; }
        public int Length { get; set; }

        public byte[] GetStream()
        {
            return null;
        }

        public IProtocol Handle(byte[] bytes)
        {
            if (bytes.Length < 24) return null;
            Head = bytes.Take(4).ToArray().ASCIIToString();
            DataLength = bytes.Skip(4).Take(4).ToArray().ByteToInt();
            Cmd = bytes.Skip(8).Take(4).ToArray().ByteToInt();
            Error = bytes.Skip(12).Take(4).ToArray().ByteToInt();
            ClientAddress = bytes.Skip(16).Take(4).ToArray().ByteToInt();
            ServerAddress = bytes.Skip(20).Take(4).ToArray().ByteToInt();
            return this;
        }
    }
}
